package cn.yeyiu.netonlinevideo.fragment;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import java.util.List;

import cn.yeyiu.netonlinevideo.adapter.GridDataAdapter;
import cn.yeyiu.netonlinevideo.adapter.ListDataAdapter;
import cn.yeyiu.util.bean.MediaData;

/**
 * 只负责一种数据类型type的视频列表list
 * 根据接受的封装list 解封后进行渲染
 */

public class GridDataFragment extends DataFragment {

    GridDataAdapter adapter;

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        }else{
            rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        }
    }

    public GridDataFragment(Context context){
        super(context);
        adapter = new GridDataAdapter(mContext);

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = super.onCreateView(inflater, container, savedInstanceState);

        rv.setAdapter(adapter);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int count = 2;
        if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
            count = 4;
        }
        rv.setLayoutManager(new StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL));

        return root;
    }

    @Override
    public void infoDataChange(List<MediaData> mediaDataList) {
        adapter.updateList(mediaDataList);
        changed();
    }
}
